[[...path]].page.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import React, { useEffect } from 'react';
  2. import { type IPagePopulatedToShowRevision, getIdForRef } from '@growi/core';
  3. import type {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import Head from 'next/head';
  8. import superjson from 'superjson';
  9. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  10. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  11. import GrowiContextualSubNavigationSubstance from '~/components/Navbar/GrowiContextualSubNavigation';
  12. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  13. import { ShareLinkPageView } from '~/components/ShareLinkPageView';
  14. import type { SupportedActionType } from '~/interfaces/activity';
  15. import { SupportedAction } from '~/interfaces/activity';
  16. import type { CrowiRequest } from '~/interfaces/crowi-request';
  17. import type { RendererConfig } from '~/interfaces/services/renderer';
  18. import type { IShareLinkHasId } from '~/interfaces/share-link';
  19. import type { PageDocument } from '~/server/models/page';
  20. import ShareLink from '~/server/models/share-link';
  21. import {
  22. useCurrentUser, useRendererConfig, useIsSearchPage, useCurrentPathname,
  23. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useIsContainerFluid, useIsEnabledMarp,
  24. } from '~/stores/context';
  25. import { useCurrentPageId, useIsNotFound, useSWRMUTxCurrentPage } from '~/stores/page';
  26. import loggerFactory from '~/utils/logger';
  27. import type { NextPageWithLayout } from '../_app.page';
  28. import type { CommonProps } from '../utils/commons';
  29. import {
  30. getServerSideCommonProps, generateCustomTitleForPage, getNextI18NextConfig, skipSSR, addActivity,
  31. } from '../utils/commons';
  32. const logger = loggerFactory('growi:next-page:share');
  33. type Props = CommonProps & {
  34. shareLinkRelatedPage?: IShareLinkRelatedPage,
  35. shareLink?: IShareLinkHasId,
  36. isNotFound: boolean,
  37. isExpired: boolean,
  38. disableLinkSharing: boolean,
  39. isSearchServiceConfigured: boolean,
  40. isSearchServiceReachable: boolean,
  41. isSearchScopeChildrenAsDefault: boolean,
  42. isEnabledMarp: boolean,
  43. drawioUri: string | null,
  44. rendererConfig: RendererConfig,
  45. skipSSR: boolean,
  46. ssrMaxRevisionBodyLength: number,
  47. };
  48. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  49. superjson.registerCustom<IShareLinkRelatedPage, string>(
  50. {
  51. isApplicable: (v): v is IShareLinkRelatedPage => {
  52. return v != null
  53. && v.toObject != null
  54. && v.lastUpdateUser != null
  55. && v.creator != null
  56. && v.revision != null;
  57. },
  58. serialize: (v) => { return superjson.stringify(v.toObject()) },
  59. deserialize: (v) => { return superjson.parse(v) },
  60. },
  61. 'IShareLinkRelatedPageTransformer',
  62. );
  63. // GrowiContextualSubNavigation for shared page
  64. // get page info from props not to send request 'GET /page' from client
  65. type GrowiContextualSubNavigationForSharedPageProps = {
  66. page?: IPagePopulatedToShowRevision,
  67. isLinkSharingDisabled: boolean,
  68. }
  69. const GrowiContextualSubNavigationForSharedPage = (props: GrowiContextualSubNavigationForSharedPageProps): JSX.Element => {
  70. const { page, isLinkSharingDisabled } = props;
  71. return (
  72. <div data-testid="grw-contextual-sub-nav">
  73. <GrowiContextualSubNavigationSubstance currentPage={page} isLinkSharingDisabled={isLinkSharingDisabled} />
  74. </div>
  75. );
  76. };
  77. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  78. useCurrentPathname(props.shareLink?.relatedPage.path);
  79. useIsSearchPage(false);
  80. useIsNotFound(props.isNotFound);
  81. useShareLinkId(props.shareLink?._id);
  82. useCurrentPageId(props.shareLink?.relatedPage._id);
  83. useCurrentUser(props.currentUser);
  84. useRendererConfig(props.rendererConfig);
  85. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  86. useIsSearchServiceReachable(props.isSearchServiceReachable);
  87. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  88. useIsEnabledMarp(props.rendererConfig.isEnabledMarp);
  89. useIsContainerFluid(props.isContainerFluid);
  90. const { trigger: mutateCurrentPage, data: currentPage } = useSWRMUTxCurrentPage();
  91. useEffect(() => {
  92. if (!props.skipSSR) {
  93. return;
  94. }
  95. if (props.shareLink?.relatedPage._id != null && !props.isNotFound) {
  96. mutateCurrentPage();
  97. }
  98. }, [mutateCurrentPage, props.isNotFound, props.shareLink?.relatedPage._id, props.skipSSR]);
  99. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName(props.shareLinkRelatedPage);
  100. const pagePath = props.shareLinkRelatedPage?.path ?? '';
  101. const title = generateCustomTitleForPage(props, pagePath);
  102. return (
  103. <>
  104. <Head>
  105. <title>{title}</title>
  106. </Head>
  107. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  108. <header className="py-0 position-relative">
  109. <GrowiContextualSubNavigationForSharedPage page={currentPage ?? props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />
  110. </header>
  111. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  112. <ShareLinkPageView
  113. pagePath={pagePath}
  114. rendererConfig={props.rendererConfig}
  115. page={currentPage ?? props.shareLinkRelatedPage}
  116. shareLink={props.shareLink}
  117. isExpired={props.isExpired}
  118. disableLinkSharing={props.disableLinkSharing}
  119. />
  120. </div>
  121. </>
  122. );
  123. };
  124. SharedPage.getLayout = function getLayout(page) {
  125. return (
  126. <>
  127. <DrawioViewerScript />
  128. <ShareLinkLayout>{page}</ShareLinkLayout>
  129. </>
  130. );
  131. };
  132. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  133. const req: CrowiRequest = context.req as CrowiRequest;
  134. const { crowi } = req;
  135. const { configManager, searchService } = crowi;
  136. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  137. props.isSearchServiceConfigured = searchService.isConfigured;
  138. props.isSearchServiceReachable = searchService.isReachable;
  139. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  140. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  141. props.rendererConfig = {
  142. isSharedPage: true,
  143. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  144. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  145. isEnabledMarp: configManager.getConfig('crowi', 'customize:isEnabledMarp'),
  146. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  147. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  148. drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
  149. plantumlUri: configManager.getConfig('crowi', 'app:plantumlUri'),
  150. // XSS Options
  151. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  152. xssOption: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
  153. attrWhitelist: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
  154. tagWhitelist: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
  155. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  156. };
  157. props.ssrMaxRevisionBodyLength = configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  158. }
  159. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  160. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  161. props._nextI18Next = nextI18NextConfig._nextI18Next;
  162. }
  163. function getAction(props: Props): SupportedActionType {
  164. let action: SupportedActionType;
  165. if (props.isExpired) {
  166. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  167. }
  168. else if (props.shareLink == null) {
  169. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  170. }
  171. else {
  172. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  173. }
  174. return action;
  175. }
  176. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  177. const req = context.req as CrowiRequest;
  178. const { crowi, params } = req;
  179. const result = await getServerSideCommonProps(context);
  180. if (!('props' in result)) {
  181. throw new Error('invalid getSSP result');
  182. }
  183. const props: Props = result.props as Props;
  184. try {
  185. const shareLink = await ShareLink.findOne({ _id: params.linkId }).populate('relatedPage');
  186. if (shareLink == null) {
  187. props.isNotFound = true;
  188. }
  189. else {
  190. props.isNotFound = false;
  191. props.isExpired = shareLink.isExpired();
  192. props.shareLink = shareLink.toObject();
  193. // retrieve Page
  194. const Page = crowi.model('Page');
  195. const relatedPage = await Page.findOne({ _id: getIdForRef(shareLink.relatedPage) });
  196. // determine whether skip SSR
  197. const ssrMaxRevisionBodyLength = crowi.configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  198. props.skipSSR = await skipSSR(relatedPage, ssrMaxRevisionBodyLength);
  199. // populate
  200. props.shareLinkRelatedPage = await relatedPage.populateDataToShowRevision(props.skipSSR); // shouldExcludeBody = skipSSR
  201. }
  202. }
  203. catch (err) {
  204. logger.error(err);
  205. }
  206. injectServerConfigurations(context, props);
  207. await injectNextI18NextConfigurations(context, props);
  208. await addActivity(context, getAction(props));
  209. return {
  210. props,
  211. };
  212. };
  213. export default SharedPage;